package edu.cqie.smartfarm.controller;


import cn.hutool.core.bean.BeanUtil;
import edu.cqie.smartfarm.constant.CommonConstant;
import edu.cqie.smartfarm.constant.MessageConstant;
import edu.cqie.smartfarm.dto.DictionaryDTO;
import edu.cqie.smartfarm.dto.DictionaryQueryDTO;
import edu.cqie.smartfarm.dto.DictionaryUpdateDTO;
import edu.cqie.smartfarm.entity.DictionaryData;
import edu.cqie.smartfarm.result.PageVO;
import edu.cqie.smartfarm.result.R;
import edu.cqie.smartfarm.service.IDictionaryDataService;
import edu.cqie.smartfarm.vo.DictionaryVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author yyw
 * @since 2024-12-26
 */
@RestController
@RequestMapping("/dictionary-data")
@RequiredArgsConstructor
@Api(tags = "字典数据")
public class DictionaryDataController {
    private final IDictionaryDataService dictionaryDataService;

    /**
     * 获取字典数据列表
     */
    @ApiOperation(value = "获取字典数据列表")
    @GetMapping("/getValue")
    @Cacheable(value = CommonConstant.PROJECT_NAME+":dictionaryDataList", key = "#dictionaryQueryDTO.dictionaryListId+':page_'+#dictionaryQueryDTO.pageNumber+'_size_'+#dictionaryQueryDTO.pageSize+'_query_'+ #dictionaryQueryDTO")
    public R<PageVO<DictionaryVO>> getValueList(@Validated DictionaryQueryDTO dictionaryQueryDTO){
        PageVO<DictionaryVO> dictionaryVOList = dictionaryDataService.getValueList(dictionaryQueryDTO);
        return R.Success(dictionaryVOList);
    }

    /**
     * 添加字典数据
     */
    @ApiOperation(value = "添加字典数据")
    @PostMapping("/addData")
    @CacheEvict(value = CommonConstant.PROJECT_NAME+":dictionaryDataList", allEntries = true)
    public R addValue(@RequestBody @Validated DictionaryDTO dictionaryDTO){
        dictionaryDataService.addValue(dictionaryDTO);
        return R.Success();
    }

    /**
     * 修改字典数据状态
     */
    @ApiOperation(value = "修改字典数据状态")
    @PutMapping("/updateStatus")
    @CacheEvict(value = CommonConstant.PROJECT_NAME+":dictionaryDataList", allEntries = true)
    public R updateStatus(@RequestBody DictionaryUpdateDTO dictionaryUpdateDTO){
        DictionaryData dictionaryData = new DictionaryData();
        dictionaryData.setStatus(dictionaryUpdateDTO.getStatus());
        dictionaryData.setId(dictionaryUpdateDTO.getDictionaryDataId());
        boolean b = dictionaryDataService.updateById(dictionaryData);
        if (!b){
            throw new RuntimeException(MessageConstant.UPDATE_FAIL);
        }
        return R.Success();
    }

    @ApiOperation(value = "获取全部字典数据列表")
    @GetMapping("/getList")
    @Cacheable(value = CommonConstant.PROJECT_NAME+":dictionaryDataList", key = "#root.methodName")
    public R<List<DictionaryVO>> getList(){
        List<DictionaryData> list = dictionaryDataService.list();
        List<DictionaryVO> dictionaryVOList = BeanUtil.copyToList(list, DictionaryVO.class);
        return R.Success(dictionaryVOList);
    }
}
